package com.atguigu.mall.mallmember.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.mall.mallmember.feign.CouponFeignService;
import com.atguigu.mall.mallmember.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.mall.mallmember.entity.MemberEntity;
import com.atguigu.mall.mallmember.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

/**
 * 会员
 *
 * @author Fuc
 * @email leftbear@gmail.com
 * @date 2020-10-21 15:53:02
 */
@RestController
@RequestMapping("mallmember/member")
public class MemberController {
  @Autowired private MemberService memberService;

  @Autowired CouponFeignService couponFeignService;

  @RequestMapping("/coupons")
  public R test() {
    MemberEntity memberEntity = new MemberEntity();
    memberEntity.setNickname("张三 ");
    R membercoupons = couponFeignService.membercoupons();
    return R.ok().put("member", memberEntity).put("coupons", membercoupons.get("coupons"));
  }

  /** 列表 */
  @RequestMapping("/list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = memberService.queryPage(params);

    return R.ok().put("page", page);
  }

  /** 信息 */
  @RequestMapping("/info/{id}")
  public R info(@PathVariable("id") Long id) {
    MemberEntity member = memberService.getById(id);

    return R.ok().put("member", member);
  }

  /** 保存 */
  @RequestMapping("/save")
  public R save(@RequestBody MemberEntity member) {
    memberService.save(member);

    return R.ok();
  }

  /** 修改 */
  @RequestMapping("/update")
  public R update(@RequestBody MemberEntity member) {
    memberService.updateById(member);

    return R.ok();
  }

  /** 删除 */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] ids) {
    memberService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

  @PostMapping("/regist")
  public R regist(@RequestBody MemberRegistVo vo) {
    memberService.regist(vo);
    return R.ok();
  }
}
